﻿var express = require('express');
var router = express.Router();
var fs = require('fs'); // 载入fs模块
var multer = require('multer');
var dateFormat = require('dateformat');

var mysqlhelper = require('../sys/sql/mysqlhelper.js');


//Post方法增加数据  
router.post('/insertpan', function (req, res) {
    //能正确解析 json 格式的post参数

    //存储数据库
    var insertsql = "INSERT INTO paninfo(panname,userid,pancount,content,memo,createtime) " +
        "VALUES(?,?,?,?,?,?);";

    var insertparams = [req.body.panname, req.body.userid, req.body.pancount, req.body.content, req.body.memo, dateFormat((new Date()), "yyyy-mm-dd hh:MM:ss")];
    //
    mysqlhelper.query(insertsql, insertparams, function (err, rows, fields) {
        if (err) {
            console.log(err);
            return;
        } else {
            res.end(JSON.stringify(rows, null, 2))

        }

    });

})

//Post方法增加数据  
router.post('/updatepan', function (req, res) {
    //能正确解析 json 格式的post参数

    //存储数据库
    var updatesql = "update paninfo set pancount=?,content=? where panid=?;";


    var updateparams = [req.body.pancount, req.body.content, req.body.panid];
    //
    mysqlhelper.query(updatesql, updateparams, function (err, rows, fields) {
        if (err) {
            console.log(err);
            return;
        } else {
            res.end(JSON.stringify(rows, null, 2))

        }

    });

})

//Post方法增加数据  
router.post('/getpanbyid', function (req, res) {
    //能正确解析 json 格式的post参数

    //存储数据库
    var selectsql = "select * from paninfo where panid=? "; //where panid=?

    var selectparams = [req.body.panid];
    //
    mysqlhelper.query(selectsql, selectparams, function (err, rows, fields) {
        if (err) {
            console.log(err);
            return;
        } else {
            res.end(JSON.stringify(rows, null, 2))
        }

    });

})

//Post方法增加数据  
router.post('/getpanbyuser', function (req, res) {
    //能正确解析 json 格式的post参数

    //存储数据库
    var selectsql = "select * from paninfo where userid=? order by panid desc; "; //

    var selectparams = [req.body.userid];
    //
    mysqlhelper.query(selectsql, selectparams, function (err, rows, fields) {
        if (err) {
            console.log(err);
            return;
        } else {
            res.end(JSON.stringify(rows, null, 2))
        }

    });

})

//Post方法增加数据  
router.post('/getpanbytype', function (req, res) {
    //能正确解析 json 格式的post参数

    //存储数据库
    var selectsql = "select * from paninfo where pantype=0 order by panid desc; "; //

    var selectparams = [];
    //
    mysqlhelper.query(selectsql, selectparams, function (err, rows, fields) {
        if (err) {
            console.log(err);
            return;
        } else {
            res.end(JSON.stringify(rows, null, 2))
        }

    });

})
//Post方法删除
router.post('/delpanbyid', function (req, res) {
    //能正确解析 json 格式的post参数

    //存储数据库
    var deletesql = "delete  from paninfo where panid=? ;"; //

    var deleteparams = [req.body.panid];
    //
    mysqlhelper.query(deletesql, deleteparams, function (err, rows, fields) {
        if (err) {
            console.log(err);
            return;
        } else {
            res.end(JSON.stringify(rows, null, 2))
        }

    });

})

var filename = "back"+new Date().getTime() + ".png";
var storage = multer.diskStorage({
    destination: function (req, file, cb) {
        //文件上传成功后会放入public下的upload文件夹
        cb(null, './public/upload')
    },
    filename: function (req, file, cb) {
        //设置文件的名字为其原本的名字，也可以添加其他字符，来区别相同文件，例如file.originalname+new Date().getTime();利用时间来区分
        cb(null, filename)//file.originalname
    }
});
var upload = multer({
    storage: storage
});
//处理来自页面的ajax请求。single文件上传
router.post('/uploadfile', upload.single('file'), function (req, res, next) {
    //拼接文件上传后的网络路径，
    var url = 'http://' + req.headers.host + '/upload/' + req.file.originalname;
    //
    //存储数据库
    var updatesql = "update    paninfo  set backimg=? where panid=? ;"; //

    var updateparams = [filename, req.query.panid];
    //
    mysqlhelper.query(updatesql, updateparams, function (err, rows, fields) {
        if (err) {
            console.log(err);
            return;
        } else {
            res.end(JSON.stringify(rows, null, 2))
        }
    });   
});
router.post('/uploadcanvas', function (req, res) {
    //接收前台POST过来的base64
    var imgData = req.body.imgdata;
    //过滤data:URL
    var base64Data = imgData.replace(/^data:image\/\w+;base64,/, "");
    var dataBuffer = new Buffer(base64Data, 'base64');
    var filename = './public/upload/title'+new Date().getTime() + ".png";
    fs.writeFile(filename, dataBuffer, function (err) {
        if (err) {
            res.send(err);
        } else {
            res.send("保存成功！");

            //存储数据库
            var updatesql = "update    paninfo  set titleimg=? where panid=? ;"; //

            var updateparams = [filename, req.body.panid];
            //
            mysqlhelper.query(updatesql, updateparams, function (err, rows, fields) {
                if (err) {
                    console.log(err);
                    return;
                } else {
                    res.end(JSON.stringify(rows, null, 2))
                }
            });   
        }
    });
});

module.exports = router;